import React, {Component, PropTypes} from 'react';
import {inject, observer} from 'mobx-react';
import * as mobx from 'mobx';
import objectAssign from 'object-assign';
import {Button, Modal, Spin} from 'antd';
import {WeaTools, WeaDialog, WeaSelect, WeaInput, WeaSearchGroup, WeaFormItem, WeaLocaleProvider} from 'ecCom';
import {WeaSwitch} from 'comsMobx';

const comTypes = WeaTools.types;
const getLabel = WeaLocaleProvider.getLabel;
const formItemLayout = {
    labelCol: {span: 6},
    fieldcol: {span: 18},
};

@inject("favouriteDialogStore", "sysFavFormStore")
@observer
export default class EditSysFavourite extends Component {
    /**
     * 关闭窗口
     */
    closeEditFormWin = () => {
        const {favouriteDialogStore, sysFavFormStore} = this.props;
        favouriteDialogStore.closeEditDialog();
        sysFavFormStore.resetForm();
    }

    onChange = data => {
        const {sysFavFormStore} = this.props;
        sysFavFormStore.saveFormFields(data);
    }

    /**
     * 获取表单的数据
     * @returns {Array}
     */
    getFormData = () => {
        const {sysFavFormStore} = this.props;
        let {pagename, favouritetype, favouriteObjid, favtypename, typelist} = sysFavFormStore;

        const items = [];
        items.push({
            colSpan: 1,
            conditionType: comTypes.INPUT,
            domkey: ['pagename'],
            isQuickSearch: false,
            label: getLabel(22426, '收藏标题'),
            value: pagename,
            viewAttr: 3,
            length: 3000,
            ...formItemLayout,
        });


        favouriteObjid = Number(favouriteObjid);
        let viewAttr = 2;
        if (favouriteObjid > 0 || Object.is(favouritetype, '6')) { // 收藏的对象存在，或者消息类型，不可编辑
            viewAttr = 1;
        }
        typelist = mobx.toJS(typelist);
        const options = [];
        typelist.forEach((item) => {
            const {id, name} = item;
            options.push({
                key: id,
                showname: name,
                selected: false,
            });
        });

        items.push({
            colSpan: 1,
            conditionType: comTypes.SELECT,
            domkey: ['favouritetype'],
            isQuickSearch: false,
            label: getLabel(22242, '收藏类型'),
            options,
            value: favouritetype,
            viewAttr,
            ...formItemLayout,
        });
        return items;
    }

    getFormItems() {
        const {sysFavFormStore} = this.props;
        const datas = this.getFormData();
        const {form} = sysFavFormStore;
        const {isFormInit} = form;
        const items = [];
        isFormInit && datas.forEach((field) => {
            items.push({
                com: (
                    <WeaFormItem
                        ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@3hqr03@${field.domkey[0]}`}
                        label={`${field.label}`}
                        labelCol={field.labelCol}
                        wrapperCol={field.fieldcol}
                    >
                        <WeaSwitch
                            ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@fui8z2@${field.domkey[0]}`}
                            fieldConfig={field} form={form} onChange={this.onChange}/>
                    </WeaFormItem>
                ),
            });
        });

        const groups = [];

        groups.push(
            <WeaSearchGroup
                ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ggkkl1@group`}
                needTigger={false} title={getLabel(81711, '基本信息')}
                showGroup items={items}
                col={1}/>
        );

        return groups;
    }

    handleSubmit = (evt) => {
        evt.preventDefault();
        const {sysFavFormStore} = this.props;
        sysFavFormStore.saveForm();
    }

    componentDidUpdate() {
        const {sysFavFormStore} = this.props;
        const datas = this.getFormData();
        const {form} = sysFavFormStore;
        const conditions = [{items: datas}];
        form.initFormFields(conditions);
    }

    render() {
        const formItems = this.getFormItems();
        const {favouriteDialogStore, sysFavFormStore} = this.props;
        const visible = favouriteDialogStore.showEditDialog;
        const title = `${getLabel(388213, '编辑收藏')}`;
        const style = {width: 460, height: 200};
        const buttons = [
            (
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@gkg1q3@submit`}
                        onClick={this.handleSubmit}>
                    {getLabel('826', '确定')}
                </Button>
            ),

            (
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@n386cr@close`}
                        onClick={this.closeEditFormWin}>
                    {getLabel('31129', '取消')}
                </Button>
            ),
        ];

        const onCancel = this.closeEditFormWin;

        const params = {visible, title, style, buttons, onCancel};

        const {spinning, tip} = sysFavFormStore;

        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@crsc3v`} {...params}>
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@ilso7t`}
                      spinning={spinning}
                      tip={tip}
                      size="large" style={{height: '100%'}}>
                    <div className="sysfav-form-container">
                        {formItems}
                    </div>
                </Spin>
            </WeaDialog>
        );
    }
}
